#include "MapParser.h"

#include "Map.h"

MapParser::MapParser():XMLParser()
{ 
	m_breedte = -1; 
	m_hoogte = -1;	


}

int MapParser::getBreedte()
{
	if(m_breedte == -1) 
	{
		char * strBreedte;
		int position = getPosition("dimensies");

		if( position != -1)
			this->getValueNode(&(nodes->at(position)),"breedte",&strBreedte);
		else 
			return -1;

		m_breedte = atoi(strBreedte);
		
	}

	return m_breedte;
	
}

int MapParser::getHoogte()
{
	if(m_hoogte == -1) 
	{
		char * strHoogte;
		int position = getPosition("dimensies");

		if( position != -1)
			this->getValueNode(&(nodes->at(position)),"hoogte",&strHoogte);
		else 
			return -1;

		m_hoogte = atoi(strHoogte);
		
	}

	return m_hoogte;
	
}

/* geeft vakje type terug ingelezen uit XMLfile */
string MapParser::getVakjeType(int x,int y)
{
	char * nameStr, *xStr, *yStr, *typeStr;
	int xNr, yNr;

	for(size_t i = 0; i <nodes->size(); i++)
	{
		getNameNode(&(nodes->at(i)),&nameStr);

		if(strcmp(nameStr,"vakje") == 0)
		{
			getValueNode(&(nodes->at(i)), "x", &xStr);
			xNr = atoi(xStr);

			getValueNode(&(nodes->at(i)), "y", &yStr);
			yNr = atoi(yStr);

			if(xNr == x && yNr == y)
			{
				getValueNode(&(nodes->at(i)), "type", &typeStr);
				return string(typeStr);
			}

		}

	}

	// indien geen specifieke waarde => return Vloer
	return string("VV");

}

#include "Bestand.h"
#include <sstream>
using std::stringstream;
using std::endl;

void MapParser::saveMap(const Map& map)
{
	stringstream bestandInhoud;
	bestandInhoud << "<xml>\n\t<head>\n\t\t<dimensies hoogte=\"" << map.height()
				  << "\" breedte=\"" << map.width() << "\"/>\n\t</head>\n\t<body>\n";

	for(size_t x = 0; x < map.width(); x++)
		for(size_t y = 0; y < map.height(); y++)
		{
			const Vakje* current = map.getVakje(x, y);
			bestandInhoud << "\t\t<vakje type=\"" << current->serialize() << "\" x=\"" << x << "\" y=\"" << y << "\" />" << endl; 			
		}

	bestandInhoud << "\t</body>\n</xml>" << endl << endl;

	Bestand bestand(string("resources/maps/") + map.getNaam() + string(".xml") );
	bestand.setData( bestandInhoud.str() );
}
